package controller;

import gui.ImageToggleButton;
import gui.OptionPanel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.media.j3d.BranchGroup;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JToggleButton;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;

import util.ImageLoader;
import controller.grid.Grid;

import math.Spherical;

public class GridController {

	private final Grid xyGrid = new Grid(new Point3d(0, 0, 0), new Spherical(0, 0), new Color3f(0, 0, 1));
	private final Grid xzGrid = new Grid(new Point3d(0, 0, 0), new Spherical(Math.PI/2, Math.PI/2), new Color3f(1, 0, 0));
	private final Grid yzGrid = new Grid(new Point3d(0, 0, 0), new Spherical(0, Math.PI/2), new Color3f(0, 1, 0));
	
	private final BranchGroup root;
	private ImageLoader loader;

	
	public GridController(BranchGroup root, CameraController camControl, OptionPanel optionPanel) {
		this.root = root;
		
		loader = new ImageLoader("/resource/image/menu/");
		
		Box box = Box.createHorizontalBox();
		box.setBorder(BorderFactory.createEmptyBorder(2, 5, 2, 5));
	
		box.add(this.createButton(xyGrid, "xyGrid.png"));
		box.add(this.createButton(xzGrid, "xzGrid.png"));
		box.add(this.createButton(yzGrid, "yzGrid.png"));
	
		optionPanel.add(box, BorderLayout.EAST);
	}
	
	private JToggleButton createButton(Grid grid, String image) {
		JToggleButton button =  new ImageToggleButton(loader.load(image));
		button.addActionListener(new GridListener(grid));
		
		return button;
	}
	
	
	
	class GridListener implements ActionListener{
		
		Grid grid;
		
		GridListener(Grid grid) {
			this.grid = grid;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (((JToggleButton)e.getSource()).isSelected()) {
				GridController.this.root.addChild(grid);
			}
			else {
				GridController.this.root.removeChild(grid);
			}
		}
	}
}
